﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace GUIL
{
    public partial class ScreenCastGUI : Form, RemoteService.IRemoteServiceCallback
    {
        RemoteService.IRemoteService _remote;
        RemoteService.Movie _movie;
        readonly RemoteService.Cinema _selectedCinema;

        public ScreenCastGUI(RemoteService.Movie movie, RemoteService.Cinema cinema)
        {
            // Hosting client interface ... (more info later)
            // Connection hasn't been opened yet!
            _remote = new RemoteService.RemoteServiceClient(new InstanceContext(this), "WSDualHttpBinding_IRemoteService");

            _movie = movie;
            _selectedCinema = cinema;

            InitializeComponent();
        }

        private void ScreenCastGUI_Load(object sender, EventArgs e)
        {
            // Add movies to comboBox
            List<RemoteService.Movie> _movies =  _remote.GetMoviesUsingCinema(_selectedCinema);
            comboBoxMovieTitles.DataSource = _movies;
            // if movie is provided, set as selected
            comboBoxMovieTitles.SelectedItem = _movie == null ? null : _movies.Single(m => m.Title == _movie.Title);

            // Add values to movie hall comboBox
            ComboBoxMovieHall.DataSource = _remote.GetMovieHalls(_selectedCinema);

            // initialize screen casts view
            Initialize_DataGridCiewScreenCasts();
        }

        private void comboBoxMovieTitles_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateDataGridViewScreenCasts();
        }

        private List<RemoteService.ScreenCast_Seat> GenerateScreenSeats() 
        {
            List<RemoteService.ScreenCast_Seat> scList = new List<RemoteService.ScreenCast_Seat>();

            foreach (RemoteService.Seat s in _remote.GetSeats((RemoteService.MovieHall)ComboBoxMovieHall.SelectedItem))
            {
                scList.Add(new RemoteService.ScreenCast_Seat
                {
                    SeatId = s.Id,
                    SeatAvailable = true
                });
            }

            return scList;
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            // Make ScreenCast
            RemoteService.ScreenCast _screenCast = new RemoteService.ScreenCast
            {
                Airdate = dateTimePicker.Value.Date + new TimeSpan((int)numericUpDownHours.Value, (int)numericUpDownMins.Value, 0),
                MovieId = _movie.Id,
                Price = Convert.ToInt32(textBoxPrice.Text),
                MovieHall = (RemoteService.MovieHall)ComboBoxMovieHall.SelectedItem,
                ScreenCast_Seat = GenerateScreenSeats()
            };
            
            // add screen cast
            _remote.AddScreenCast(_screenCast);

            // Update table!
            UpdateDataGridViewScreenCasts();

        }

        /// <summary>
        /// Initialize screen casts data grid view manually
        /// </summary>
        private void Initialize_DataGridCiewScreenCasts()
        {
            dataGridViewScreenCasts.ColumnCount = 4;
            dataGridViewScreenCasts.Columns[0].Name = "Id";
            dataGridViewScreenCasts.Columns[1].Name = "Airdate";
            dataGridViewScreenCasts.Columns[2].Name = "Movie hall";
            dataGridViewScreenCasts.Columns[3].Name = "Price";


            dataGridViewScreenCasts.Columns[1].Width = 200;
            dataGridViewScreenCasts.Columns[2].Width = 200;
            dataGridViewScreenCasts.Columns[3].Width = 190;


            dataGridViewScreenCasts.Columns[0].Visible = false; // hide first column, used internal
        }

        /// <summary>
        /// Update screen casts data grid view
        /// </summary>
        private void UpdateDataGridViewScreenCasts()
        {
            // Get movie object from comboBox
            _movie = (RemoteService.Movie)comboBoxMovieTitles.SelectedItem;

            // Get screenCasts related to movie                             
            _movie.ScreenCasts = _remote.GetScreenCasts(_movie, _selectedCinema);

            // remove current screen casts
            dataGridViewScreenCasts.Rows.Clear();
            // Add screenCasts to dataGrid
            foreach (var item in _movie.ScreenCasts)
            {
                // get movie hall object
                RemoteService.MovieHall _movieHall = _remote.GetMovieHallUsingScreenCast(item);
                dataGridViewScreenCasts.Rows.Add(new object[] { item.Id, item.Airdate, _movieHall.Number, item.Price });
            }
        }


        public void OnAction(List<RemoteService.ScreenCast_Seat> screenSeats)
        {
            throw new NotImplementedException();
        }
    }
}
